<?php
	ob_start();	
include_once('../../app/connection.php');

if(isset($_POST['action']))
{
    $action = $_POST['action'];
    
    if($action == "get_malop" && $_POST['mamh'] != "")
    {
        $mamh = $_POST['mamh'];
        $nam = $_POST['nam'];
        $hocky = $_POST['hocky'];
            
        $sql = "SELECT MALOP FROM lop WHERE MAMONHOC = '$mamh' AND HOCKY = $hocky AND NAM = '$nam'";
        $result = mysql_query($sql);
        
        while($rows = mysql_fetch_assoc($result))
        {
            echo "<option value='".$rows['MALOP']."'>".$rows['MALOP']."</option>";               
        }
    }
    
    if($action == "get_mamh")
    {
        $nam = $_POST['nam'];
        $hocky = $_POST['hocky'];
            
        $sql = "SELECT MAMONHOC FROM lop WHERE HOCKY = $hocky AND NAM = '$nam' GROUP BY MAMONHOC";
        $result = mysql_query($sql);
        
        while($rows = mysql_fetch_assoc($result))
        {
            echo "<option value='".$rows['MAMONHOC']."'>".$rows['MAMONHOC']."</option>";               
        }
    }
    
    if($action == "Update")
    {
        $mamh = $_POST['mamh'];
        $malop = $_POST['malop'];
        $nam = $_POST['nam'];
        $hocky = $_POST['hocky'];
		$ca = $_POST['ca'];
        
        $sql = "UPDATE lop SET MAGIAOVIEN='".$_POST['magv']."', MALOAI='".$_POST['maloai']."', 
            THU ='".$_POST['thu']."', CA ='".$_POST['ca']."', PHONG ='".$_POST['phong']."',
            TOIDA ='".$_POST['toida']."', TRANGTHAI ='".$_POST['trangthai']."'
            WHERE MAMONHOC='$mamh' AND MALOP = '$malop' AND HOCKY = '$hocky' AND NAM = '$nam'";
            $result = mysql_query($sql);
			if(!$result)
			{
				$rep = mysql_error();
				echo "$rep";
				echo "<meta http-equiv=\"refresh\" content=\"0; URL=../?page=dkmh&function=chinhsua\">";
			}
        
            else header('Location: ../?page=dkmh&function=ds_lop');
    }  
}
?>